<script>
import req from "@/utils/request"
export default {
  name: "Login",
  data(){
    const validatePassword = (rule, confirmPassword, callback) => {
      if (confirmPassword === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPassword !== this.user.password){
        callback(new Error('密码不一致'))
        callback()
      } else {
        callback()
      }
    }

    //验证码校验
    return {
      user: {
        username: '',
        password: '',
        confirmPassword: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPassword: [
          { validator: validatePassword, trigger: 'blur' }
        ],
      },
    }
  },
  methods:{
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          (async () => {
            const res = await req.post("/register", this.user) //this.user对应后端接收请求的请求体参数，如果不给就报错
            if(res.code === '500'){
              this.$message.error(res.msg)
            }else {
              this.$router.push('/Login')
              this.$message.success('注册成功')
            }
            console.log(res)
          })()
        }
      })
    }
  }
}
</script>

<template>
  <div style="height: 100vh; display: flex; justify-content: center; align-items: center; background-color: #0d538d">
    <div style="display: flex; background-color: #fff; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/img/LoginImg.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 28px">欢迎注册后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="register">注册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已有账号?请<span style="color: #6e77f2; cursor: pointer" @click="$router.push('/Login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>

  </div>
</template>

<style scoped>

</style>